<?php
namespace app\index\controller;

use app\common\model\Configs;
use app\common\model\SystemMenu;

/**
 * 系统配置
 * Class System
 * @package app\agent\controller
 */
class System extends Base
{
    /*
	 * 配置入口
	 */
    public function index()
    {
        /*配置列表*/
        $group_list = array('website_info'=>'网站信息','alisms'=>'短信设置','pay'=>'支付设置','entpay'=>'企业付款');
        $this->assign('group_list',$group_list);
        $inc_type = $this->request->param('inc_type','website_info'); //配置参数
        $this->assign('inc_type',$inc_type);
        $res = Configs::where('inc_type',$inc_type)->select();
        $config=null;
        foreach($res as $v){
            $config[$v['name']] = $v['value'];
        }
//        $config = tpCache($inc_type);//更新缓存
//        if($inc_type == 'shop_info'){
//            $province = M('region')->where(array('parent_id'=>0))->select();
//            $city =  M('region')->where(array('parent_id'=>$config['province']))->select();
//            $area =  M('region')->where(array('parent_id'=>$config['city']))->select();
//            $this->assign('province',$province);
//            $this->assign('city',$city);
//            $this->assign('area',$area);
//        }

        $this->assign('config',$config);//当前配置项
        //C('TOKEN_ON',false);
        return $this->fetch($inc_type);
    }

    /**
     * 网站信息
     */
    public function website_info(){
        $param = $this->request->except('__token__');
        $file = $this->request->file('file');//文件是否上传
        if($file){
            //验证通过，并移动图片
            $fileName = $file->validate(['ext'=>'png,jpg,jpeg'])->move('public/upload/logo'.DIRECTORY_SEPARATOR);
            if($fileName){
                $param['store_logo']  = $fileName->getPathName();
            }
        }
        $inc_type = $param['inc_type'];
        unset($param['inc_type']);
        tpCache($inc_type,$param);//调用函数写入跟新数据
        $this->redirect('index');
    }

    /*
     * 短信设置
     */
    public function alisms(){
        $param = $this->request->except('__token__');
        $inc_type = $param['inc_type'];
        unset($param['inc_type']);
        tpCache($inc_type,$param);//调用函数写入跟新数据
        $this->redirect('index',['inc_type'=>$inc_type]);
    }

    /**
     * 支付设置
     */
    public function pay(){
            $param = $this->request->except('__token__');
        $inc_type = $param['inc_type'];
        unset($param['inc_type']);
        tpCache($inc_type,$param);//调用函数写入跟新数据
        $this->redirect('index',['inc_type'=>$inc_type]);
    }

    /**
     * 企业付款设置
     */
    public function entpay(){
            $param = $this->request->except('__token__');
        $inc_type = $param['inc_type'];
        unset($param['inc_type']);
        tpCache($inc_type,$param);//调用函数写入跟新数据
        $this->redirect('index',['inc_type'=>$inc_type]);
    }

    /**
     * 短信发送测试功能
     */
    public function smsphone()
    {
        $phone = $this->request->param('phone');
        if(!$phone){
            return $this->error('失败');
        }

        //获取短信配置
        $config = Configs::where('inc_type','alisms')->select();
        foreach($config as $v){
            $sms[$v['name']] = $v['value'];
        }
        $appid = $sms['sms_appkey'];
        $secret = $sms['sms_secretKey'];
        $phone = $phone;
        $signName = $sms['sms_product'];
        $templateCode = $sms['sms_code'];
        $code = '123';
        $commont = 'sdfas';

        //调用短信发送借口
        $query = sendSms($appid,$secret,$phone,$signName,$templateCode,$code,$commont);
        dump($query);
    }

    /**
     * 权限资源设置
     */
    public function right_list()
    {
        $name = $this->request->param('name');
        $menu = new SystemMenu();
        $list = $menu->names($name)->where('sm_type','agent')->order('sm_group','desc')->paginate(10,false,[
            'type'     => 'bootstrap',
            'path'     => url('system/right_list',['name'=>$name]),
    ]);
        $page = $list->render();
        $this->assign('list',$list);
        $this->assign('rolemenu',getRoleMenu());//权限资源分组
        $this->assign('page',$page);
        return $this->fetch('right_menu');
    }

    /**
     * 加载权限资源修改界面
     */
    public function add_right(){
        $id = $this->request->param('sm_id',0);
        $info = SystemMenu::get($id);
        if($info){
        $info['sm_right'] = explode(',',$info['sm_right']);
        }
        //获取controller 目录
        $filePath = APP_PATH.'agent/controller';
        $planList = array();
        $dirRes = opendir($filePath);
        while($dir = readdir($dirRes)){
            if(!in_array($dir,array('.','..','.svn','.git'))){
                $planList[] = basename($dir,'.php');
            }
        }
        $this->assign('planList',$planList);//目录资源
        $this->assign('rolemenu',getRoleMenu());//权限资源分组
        $this->assign('info',$info);
        return $this->fetch();
    }

    /**
     * 权限资源增加修改
     */
    public function submit_right()
    {
        if($this->request->method() === "POST"){
            if($id = $this->request->post('id')){
                $menu = SystemMenu::get($id);
                $menu->sm_name = $this->request->post('name');
                $menu->sm_group = $this->request->post('group');
                $menu->sm_type = $this->request->post('type');
                $menu->sm_right = implode(',',$this->request->post('right/a'));
                $output = $menu->save();
                $msg = '更新';
            }else{
                $menu = new SystemMenu();
                $menu->sm_name = $this->request->post('name');
                $menu->sm_group = $this->request->post('group');
                $menu->sm_type = $this->request->post('type');
                $menu->sm_right = implode(',',$this->request->post('right/a'));
                $output = $menu->save();
                $msg = '添加';
            }
            if($output){
                $this->success($msg.'操作成功',url('system/add_right',['sm_id'=>$id]),'',1);
            }else{
                $this->error($msg.'操作失败！');
            }
        }
        $this->request->error('非法请求操作！');
    }

    /**
     * 权限资源删除
     */
    public function del_right(){
        if($this->request->method() === 'POST'){
            $id = $this->request->post('sm_id');
            $output = SystemMenu::destroy($id);
            if($output){
                return $this->success('删除操作成功！');
            }
        }
        return $this->error('删除失败！');
    }
    /**
     * 获取相应控制器内的方法
     * 用于权限资源添加
     */
    public function ajax_get_action()
    {
        if($this->request->isAjax()){
            $control = $this->request->get('controller');
            $advContrl = get_class_methods("app\\agent\\controller\\".str_replace('.php','',$control));
            $baseContrl = get_class_methods('app\admin\controller\Base');
            $diffArray  = array_diff($advContrl,$baseContrl);
            $html = '';
            foreach ($diffArray as $val){
                $html .= "<option value='".$val."'>".$val."</option>";
            }
            exit($html);
        }
    }
}